package com.pink;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class RobotEx extends Robot {
    private Clipboard clipboard;
    public RobotEx() throws AWTException {
        this.clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public RobotEx(GraphicsDevice screen) throws AWTException {
        super(screen);
        this.clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public void moveAndClick(int x, int y){
        super.mouseMove(x,y);
        super.delay(500);
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(1000);
    }
    public void moveAndClickQuick(int x, int y){
        super.mouseMove(x,y);
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(800);
    }
    public void click(){
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(1000);
    }
    public void click(int ms){
        super.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        super.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        super.delay(ms);
    }
    public void input(String content){
        clipboard.setContents(new StringSelection(content),null);
        super.keyPress(KeyEvent.VK_CONTROL);
        super.keyPress(KeyEvent.VK_V);
        super.keyRelease(KeyEvent.VK_V);
        super.keyRelease(KeyEvent.VK_CONTROL);
        super.delay(1000);
    }
    public double distance(Color c1,Color c2){
        float[] f1 = c1.getRGBColorComponents(null);
        float[] f2 = c2.getRGBColorComponents(null);
        double dr= f1[0]-f2[0];
        double dg= f1[1]-f2[1];
        double db= f1[2]-f2[2];
        return Math.sqrt(dr*dr+dg*dg+db*db);

    }
    public void waitThenClick(int x,int y ,Color c){
        while (true){
            if (super.getPixelColor(x,y).equals(c)){
                break;
            }
            super.delay(2000);
        }
        this.moveAndClick(x,y);
    }
    public void keepClickUntil(int x,int y ,Color c){
        while (true){
            this.moveAndClickQuick(x, y);
            if (super.getPixelColor(x,y).equals(c)){
                break;
            }
        }
    }


}
